package com.north.light.androidxvp.ui.main

import android.app.Activity
import android.content.Intent
import android.os.Environment
import android.widget.ImageView
import com.marvhong.videoeditor.VideoEditFun
import com.marvhong.videoeditor.listener.VideoEditFunCallback
import com.marvhong.videoeditor.listener.VideoEditFunPicCallback
import com.north.light.androidxvp.R
import com.north.light.androidxvp.base.BaseActivity
import com.north.light.androidxvp.presenter.main.PMainPresenter
import com.north.light.androidxvp.protect.AppStatus
import com.north.light.androidxvp.ui.welcome.WelcomeActivity
import com.north.light.libcommon.log.KtLogUtil
import com.north.light.libloadpic.ImageManager
import com.north.light.libmvp.router.XRouter

class MainActivity : BaseActivity<PMainPresenter>() {

    companion object {
        //0默认启动
        val DATA_TYPE = "DATA_TYPE"

        fun launch(from: Activity?, type: Int) {
            XRouter.getInstance().newRouter()
                    .putInt(DATA_TYPE, type)
                    .launch(from, MainActivity::class.java)
        }
    }

    override fun createPresenter(): PMainPresenter {
        return PMainPresenter()
    }

    override fun getLayout(): Int {
        return R.layout.activity_main
    }

    override fun initData() {
        VideoEditFun.getInstance().init(this)
        VideoEditFun.getInstance().setOnPicCallback(object : VideoEditFunPicCallback {
            override fun loadImage(path: String?, view: ImageView?) {
                ImageManager.getInstance().loadNormalPic(path, view)
            }

            override fun loadImage(resId: Int, view: ImageView?) {
                ImageManager.getInstance().loadLocalPic(resId, view)
            }
        })
        VideoEditFun.getInstance().setOnFunCallback(object : VideoEditFunCallback {
            override fun addTxt(path: String?) {
                KtLogUtil.d("视频信息addTxt：${path}")
            }

            override fun error(message: String?) {
                KtLogUtil.d("视频信息error：${message}")
            }

            override fun cropPath(path: String?) {
                KtLogUtil.d("视频信息cropPath：${path}")
            }
        })
        val path = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/share_d4d8c1b6646faa1527eac44c252dd121.mp4"
//        val path = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/VID_20220321_213020.mp4"
//        val path = Environment.getExternalStorageDirectory().getPath() + "/DCIM/Camera/VID_20220322_172210.mp4"
//        val path = Environment.getExternalStorageDirectory().getPath() +
//                "/Screenrecorder-2022-03-03-14-43-16-450.mp4"
//        VideoEditFun.getInstance().cropVideo(this, path)
//        VideoEditFun.getInstance().addText(this, path)
        VideoEditFun.getInstance().snapVideo(this,path,1000L)
    }

    /**
     * 启动已经存在的activity,该方法会执行
     * */
    override fun onNewIntent(intent: Intent?) {
        super.onNewIntent(intent)
        //改activity启动模式为single task，需要set intent
        //再次启动activity时，若为后台清空activity状态，则直接打开启动页面，重新走启动流程，防止空指针行为
        setIntent(intent)
        var action = intent?.getIntExtra(AppStatus.KEY_HOME_ACTION, AppStatus.ACTION_BACK_TO_HOME)
        if (action == AppStatus.ACTION_RESTART_APP) {
            startActivity(Intent(this, WelcomeActivity::class.java))
            finish()
            return
        }
    }


}
